package com.local.controller.index;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.threads.UploadMemberInfoRunble;
import com.cz073586.tools.util.Log4jUtil;
import com.cz073586.tools.util.ThreadPoolUtil;
import com.local.manager.cfgManager.CloudConfigManager;
import com.local.model.LocalMember;
import com.local.model.LocalPayBill;
import com.local.model.ParklotNumber;
import com.local.service.face.CarInAndOutService;
import com.local.service.face.LocalTemporaryCheckService;
import com.local.service.face.LogService;
import com.local.service.face.ParklotNumberService;
import com.local.service.face.ParkwellMemberService;
import com.local.service.face.ParkwellPayBillService;
import com.other.controller.entity.CarParkIn;
import com.qianhai.common.constants.CommonContans;
import com.qianhai.common.controller.BaseController;
import com.qianhai.common.exception.BaseException;
import com.qianhai.common.utils.Tools;
import com.qianhai.common.utils.UUIDGenerateUtil;

/**
 * 类描述:这里是类描述
 * 
 * @ClassName: TestController
 * @author Administrator
 * @date 2015年4月23日 下午3:25:18
 */
@RestController
@RequestMapping(value = "/parkIn", method = RequestMethod.POST)
public class CarParkInController extends BaseController
{
	
	protected Log4jUtil log = Log4jUtil.getLog4j(this.getClass());
	
	@Autowired
	private ParkwellMemberService parkwellMemberService;
	
	@Autowired
	private CarInAndOutService carInAndOutService;
	
	@Autowired
	private LocalTemporaryCheckService localTemporaryCheckService;
	
	@Autowired
	private ParkwellPayBillService parkwellPayBillService;
	
	@Autowired
	private ParklotNumberService parklotNumberService;
	
	@Autowired
	private LogService logService;
	
	
	@RequestMapping(value = "/parkIn.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap parkIn(@RequestBody CarParkIn carParkIn,
			HttpServletRequest request)
	{
		ModelMap resultMap = new ModelMap();
		try
		{
			if (carParkIn == null)
			{
				throw new BaseException("10000-00006");
			}
				if (log.isDebugEnabled())
				{
					log.info(carParkIn.toString());
				}
			
			carInAndOutService.parkIn(returnCarParkIn(carParkIn));
			// resultMap.put(CommonContans.STRUTS_DATA, carParkInVO);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * 方法描述：同步会员信息<增，改>
	 * @Title: memberInfo 
	 * @param @param carParkIn
	 * @param @param request
	 * @param @return
	 * @return ModelMap
	 * @user Administrator
	 * 2016年1月5日
	 * @throws
	 */
	@RequestMapping(value = "/memberInfo.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap memberInfo(@RequestBody LocalMember member){
		ModelMap resultMap = new ModelMap();
		try
		{
			if (member == null)
			{
				throw new BaseException("10000-00006");
			}
			//根据当前操作用户，获取当前停车场。
			parkwellMemberService.saveOrUpdateOtherMember(member);
			
			//会员车主信息，上传到云平台
			if (CloudConfigManager.instance().isOpen())
			{
				member.setParkCode(CloudConfigManager.instance().getExchangeId());
				UploadMemberInfoRunble upload = new UploadMemberInfoRunble(
						member, logService);
				ThreadPoolUtil.instance().execute(upload);
			}
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_ERROR);
		}finally{
			return resultMap;
		}
	}
	
	
	/**
	 * 
	 * 方法描述：第三方平台，现金流水，上传到本地----到云
	 * @Title: addOtherPayBill 
	 * @param @param parkwellPayBill
	 * @param @return
	 * @return ModelMap
	 * @user Administrator
	 * 2016年1月5日
	 * @throws
	 */
	@RequestMapping(value = "/addOtherPayBill.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap addOtherPayBill(@RequestBody LocalPayBill parkwellPayBill){
		ModelMap resultMap = new ModelMap();
		try
		{
			if (parkwellPayBill == null)
			{
				throw new BaseException("10000-00006");
			}
			//根据当前操作用户，获取当前停车场。
			parkwellPayBillService.addOtherPayBill(parkwellPayBill);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_ERROR);
		}finally{
			return resultMap;
		}
	}
	
	/**
	 * 
	 * 方法描述：F3 实时 告知车位数量
	 * @Title: getParkLeft 
	 * @param @param parklotNumber
	 * @param @return
	 * @return ModelMap
	 * @user Administrator
	 * 2016年1月8日
	 * @throws
	 */
	@RequestMapping(value = "/getParkLeft.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap getParkLeft(@RequestBody ParklotNumber parklotNumber)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			
			parklotNumberService.saveOrUpdateNumber(parklotNumber);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
			
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	public com.local.model.CarParkIn returnCarParkIn(CarParkIn carParkIn){
		com.local.model.CarParkIn cc=new com.local.model.CarParkIn();
		cc.setUuid(UUIDGenerateUtil.generate());
		cc.setCarNo(carParkIn.getCarNo());
		cc.setCreateTime(new Date(System.currentTimeMillis()));
		cc.setPic1(!Tools.isEmpty(carParkIn.getPic_1())?carParkIn.getPic_1():null);
		cc.setPic2(!Tools.isEmpty(carParkIn.getPic_2())?carParkIn.getPic_2():null);
		String exchanuui=CloudConfigManager.instance().getCfgValueByKey("exchange_id");
		cc.setExchangeUuid(exchanuui);
		cc.setEquipment(!Tools.isEmpty(carParkIn.getDeviceNo())?carParkIn.getDeviceNo():null);
		cc.setOpenTime(!Tools.isEmpty(carParkIn.getCarInTime())?carParkIn.getCarInTime():null);
		return cc;
	}
	
	
}
